﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace Windows7SlideshowWallpaperUtil
{
    public partial class MainComponent : Component
    {
        private WallpaperData data = new WallpaperData();
        private ApplicationContext appContext = new ApplicationContext();
        public WallpaperData Data { get { return data; } }
        public ApplicationContext AppContext { get { return appContext; } }
        private Form1 form = null;
        public MainComponent()
        {
            InitializeComponent();
            toolStripMenuItem1.Click += new EventHandler(toolStripMenuItem1_Click);
            toolStripMenuItem2.Click += new EventHandler(toolStripMenuItem2_Click);
            toolStripMenuItem3.Click += new EventHandler(toolStripMenuItem3_Click);
            notifyIcon1.DoubleClick += new EventHandler(notifyIcon1_Click);
            appContext.ThreadExit += new EventHandler(appContext_ThreadExit);
            timer1.Start();
            showWindow();
            form.FormClosing += new FormClosingEventHandler(form_FormClosing);
        }

        void form_FormClosing(object sender, FormClosingEventArgs e)
        {
            notifyIcon1.ShowBalloonTip(4000, "Still running", "Wallpaper monitor is still running\n To open the window again double click this icon,\n For options right click this icon.", ToolTipIcon.Info);
        }

        void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Windows7SlideshowWallpaperUtil.Properties.Settings settings= Windows7SlideshowWallpaperUtil.Properties.Settings.Default;
            folderBrowserDialog1.SelectedPath = settings.FavoriteDirectory;
            if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
            {
                settings.FavoriteDirectory = folderBrowserDialog1.SelectedPath;
                settings.Save();
            }
        }

        void appContext_ThreadExit(object sender, EventArgs e)
        {
            notifyIcon1.Visible = false;
            timer1.Stop();
        }

        void notifyIcon1_Click(object sender, EventArgs e)
        {
            showWindow();
        }

        void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            appContext.ExitThread();
        }

        void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            showWindow();
        }

        private void showWindow()
        {
            Form1 form1 = this.form;
            if (form1 != null && (!form1.IsDisposed || !form1.Visible))
            {
                try
                {
                    form1.Close();
                }
                catch (Exception) { }
            }
            form1 = new Form1(Data);
            form1.Visible = true;
            this.form = form1;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Desktop\\General");
            data.setWallpaper(key.GetValue("WallpaperSource").ToString());
        }
    }
}
